package servlet;

import domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author admin
 * @Date 2019/3/4 9:09
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //User u = service.findByUsernameAndPasswrod(username,password);
        User u = null;
        if (username.equals("zs")){
            u = new User();
            u.setUsername("zs");
        }
        HttpSession session = request.getSession();
        if (u != null){ //如果登录成功，则用户信息要保存下来？？session
             session.setAttribute("user",u);
             //主页 重定向
            String path = request.getContextPath(); //项目名
            response.sendRedirect(path+"/index.jsp");


        }else { //登录失败，错误信息提示
           request.setAttribute("msg","用户名或密码错误！");
           request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}
